﻿using AutoMapper.Internal.Mappers;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Dynamic.Core;
using System.Text;
using System.Threading.Tasks;
using Volo.Abp.Application.Dtos;
using Volo.Abp.Application.Services;
using Volo.Abp.Domain.Repositories;

namespace Himp.TaskScheduling
{
    /// <summary>
    /// 业务流配置
    /// </summary>
    public class BizflowConfigService : CrudAppService<BizflowConfig, BizflowConfigDto, Guid, BizflowConfigInput, BizflowConfigCreateDto, BizflowConfigUpdateDto>, IBizflowConfigService
    {
        private IRepository<BizflowConfig, Guid> _bizflowConfigRepository;

        /// <summary>
        /// 
        /// </summary>
        /// <param name="bizflowConfigRepository"></param>
        public BizflowConfigService(IRepository<BizflowConfig, Guid> bizflowConfigRepository) : base(bizflowConfigRepository)
        {
            _bizflowConfigRepository = bizflowConfigRepository;
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        public override async Task<PagedResultDto<BizflowConfigDto>> GetListAsync(BizflowConfigInput input)
        {
            var iQuery = await _bizflowConfigRepository.GetQueryableAsync();

            if (input.Pi == 0)
            {
                var itemsDto = ObjectMapper.Map<List<BizflowConfig>, List<BizflowConfigDto>>(iQuery.ToList());

                return new PagedResultDto<BizflowConfigDto>(itemsDto.Count, itemsDto);
            }
            else
            {
                var totalCount = iQuery.Count();
                var items = iQuery.Page(input.Pi, input.Ps);
                var itemsDto = ObjectMapper.Map<List<BizflowConfig>, List<BizflowConfigDto>>(items.ToList());

                return new PagedResultDto<BizflowConfigDto>(totalCount, itemsDto);
            }
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public override async Task<BizflowConfigDto> GetAsync(Guid id)
        {
            var processType = await _bizflowConfigRepository.GetAsync(id);
            var itemDto = ObjectMapper.Map<BizflowConfig, BizflowConfigDto>(processType);

            return itemDto;
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        public override async Task<BizflowConfigDto> CreateAsync(BizflowConfigCreateDto input)
        {
            BizflowConfig processType = ObjectMapper.Map<BizflowConfigCreateDto, BizflowConfig>(input);
            processType = await _bizflowConfigRepository.InsertAsync(processType);
            var itemDto = ObjectMapper.Map<BizflowConfig, BizflowConfigDto>(processType);

            return itemDto;
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="id"></param>
        /// <param name="input"></param>
        /// <returns></returns>
        public override async Task<BizflowConfigDto> UpdateAsync(Guid id, BizflowConfigUpdateDto input)
        {
            var processType = await _bizflowConfigRepository.GetAsync(id);
            processType = ObjectMapper.Map(input, processType);
            var itemDto = ObjectMapper.Map<BizflowConfig, BizflowConfigDto>(processType);

            return itemDto;
        }
    }
}
